$(function () {
    $.get(
        'goodsAndShoppingCart/getShoppingCart.php', {
            "vipName": getCookie('username')
        },
        function (data) {
            let arr = JSON.parse(data);
            console.log(arr);
            console.log(arr[0].goodsImg)

            let oTr = "";
            for (let i = 0; i < arr.length; i++) {
                oTr =
                    `<tr class="firstRow">
                    <td class="TrOne">
                        <input type="checkbox">
                    </td>
                    <td class="TrTwo">
                        <img src="${arr[i].goodsImg}" alt="">
                        <span>${arr[i].goodsName}</span>
                    </td>
                    <td class="TrThree">
                        <span>${arr[i].goodsPrice}</span>
                    </td>
                    <td class="TrFour">
                        <button class="reduce">-</button><span>1</span><button class="add">+</button>                   
                    </td>
                    <td class="TrFive">
                        <span>${arr[i].goodsPrice}</span>
                    </td>
                    <td class="TrSix">
                        <button>删除</button>
                    </td>
                </tr>
                `
                $("table").append(oTr);
                $(".add").on("click", function () {
                    let count = $(this).prev().html();
                    count++;
                    $(this).prev().html(count);
                    let pri = $(this).parent().prev().children().html();
                    let money = pri * count;
                    $(this).parent().next().children().html(money);
                    console.log(pri);
                })
                $(".reduce").on("click", function () {
                    let count = $(this).next().html();
                    count--;
                    if (count == 0) {
                        count = 1
                    }
                    $(this).next().html(count);
                    let toPri = $(this).parent().next().children().html();
                    console.log(toPri);
                    let to = toPri / count;
                    $(this).parent().next().children().html(parseInt(to))
                })
            }
        });
});

// 全选框
$("#allSele").click(function () {
    var checkboxs = $("#allSele").parent().parent().parent().find('input[type="checkbox"]');
    if ($(this).is(':checked')) {
        checkboxs.prop("checked", true);
    } else {
        checkboxs.prop("checked", false);
    }
    // totalMoney();
});